using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;


namespace Mundo_3D
{
    class Camara_Libre : Camara_Basica
    {
        MouseState prevMouseState;
        float speed = 7;
        float currentPitch = 0;
        float totalPitch = MathHelper.PiOver4;

        public Camara_Libre (Game game, Vector3 pos, Vector3 target, Vector3 up) : base(game)
        {
            cameraPosition = pos;
            cameraDirection = target - pos;
            cameraDirection.Normalize();
            cameraUp = up;
            this.CreateLookAt();
                 
        }

        public override void Initialize(){
            Mouse.SetPosition(Game.Window.ClientBounds.Width / 2, Game.Window.ClientBounds.Height / 2);
            prevMouseState = Mouse.GetState();
            base.Initialize();
        }
        
        public override void Update(GameTime gameTime, int TipoCam, Vector3 pos){

            if (TipoCam == 0){
                if (Keyboard.GetState().IsKeyDown(Keys.W))
                    cameraPosition += cameraDirection * speed;
                if (Keyboard.GetState().IsKeyDown(Keys.S))
                    cameraPosition -= cameraDirection * speed;
                if (Keyboard.GetState().IsKeyDown(Keys.A))
                    cameraPosition += Vector3.Cross(cameraUp, cameraDirection) * speed;
                if (Keyboard.GetState().IsKeyDown(Keys.D))
                    cameraPosition -= Vector3.Cross(cameraUp, cameraDirection) * speed;

                if (Mouse.GetState().X >= 0 && Mouse.GetState().Y >= 0 &&
                    Mouse.GetState().X <= Game.Window.ClientBounds.Width && Mouse.GetState().Y <= Game.Window.ClientBounds.Height)
                {
                    cameraDirection = Vector3.Transform(cameraDirection,Matrix.CreateFromAxisAngle(cameraUp,(-MathHelper.PiOver4 / 150)*(Mouse.GetState().X - prevMouseState.X)));
                              
                    float pitchAngle = (MathHelper.PiOver4 / 150) *  (Mouse.GetState().Y - prevMouseState.Y);

                    if (Math.Abs(currentPitch + pitchAngle) < totalPitch)
                    {
                        cameraDirection = Vector3.Transform(cameraDirection,Matrix.CreateFromAxisAngle(Vector3.Cross(cameraUp, cameraDirection), pitchAngle));
                        currentPitch += pitchAngle;
                    }
                }
                       
                Mouse.SetPosition(base.Game.Window.ClientBounds.Width / 2, base.Game.Window.ClientBounds.Height / 2);

                prevMouseState = Mouse.GetState();
            }
            this.CreateLookAt();
            base.Update(gameTime);           
        }

        public override void CreateLookAt(){
            view = Matrix.CreateLookAt(cameraPosition, cameraPosition + cameraDirection, cameraUp);
        }
    }
}